/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lu.med.praxiswahl.wahlumed.service.beans;

import java.util.Calendar;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import lu.med.praxiswahl.wahlumed.dao.beans.daointerface.DoctorLettersDAOInt;
import lu.med.praxiswahl.wahlumed.model.beans.CustomerTBL;
import lu.med.praxiswahl.wahlumed.model.beans.DoctorLettersTBL;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.CustomerServiceInt;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorLetterServiceInt;

/**
 * 
 * @author pkendzo
 */
@Stateless
@Remote({ DoctorLetterServiceInt.class })
public class DoctorLetterService implements DoctorLetterServiceInt {

	private DoctorLettersTBL tmpDocLetter;
	@EJB
	private DoctorLettersDAOInt docLetterDao;

	@EJB
	private CustomerServiceInt cust;

	public DoctorLetterService() {

		cust = new CustomerService();
		tmpDocLetter = new DoctorLettersTBL();
		// docLetterDao = context_factory.getContextDoctorsLettersDao();

	}

	@Override
	public int createDoctorLetter(Calendar date, String matricule,
			String bezeichner, String text, String typ) {
		DoctorLettersTBL tmpDocLetter_1 = new DoctorLettersTBL();

		tmpDocLetter_1.setDatum(date);
		tmpDocLetter_1.setCustomer(cust.getCustomer(matricule));
		tmpDocLetter_1.setBezeichner(bezeichner);
		tmpDocLetter_1.setText(text);
		tmpDocLetter_1.setTyp(typ);

		docLetterDao.persist(tmpDocLetter_1);
		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public int deleteDoctorLetter(Long id) {
		docLetterDao.delete(id);

		return RET_OPERATION_DONE_PROPERLY;
	}

	@Override
	public void getA_DoctorLetterInstance(DoctorLettersTBL docLetter) {
		this.tmpDocLetter = docLetter;

	}

	@Override
	public String getBezeichner() {
		return tmpDocLetter.getBezeichner();
	}

	@Override
	public Calendar getDate() {
		return tmpDocLetter.getDatum();
	}

	@Override
	public Long getDoctorLetter_nummer() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CustomerTBL getPatientin() {
		return tmpDocLetter.getCustomer();
	}

	@Override
	public Long getSubPrimaryKeyIndex() {
		return docLetterDao.findDatabaseSubPrimaryKeyIndex();
	}

	@Override
	public String getText() {
		return tmpDocLetter.getText();
	}

	@Override
	public DoctorLettersTBL[] listALLCustomerDoctorLetterBy_Customer(
			String matricule) {
		List<DoctorLettersTBL> tmp = docLetterDao
				.findDocLetterByCustomer(matricule);
		DoctorLettersTBL[] tmp_1 = new DoctorLettersTBL[tmp.size()];

		for (int i = 0; i < tmp_1.length; i++) {
			tmp_1[i] = tmp.get(i);
		}
		return tmp_1;
	}

	@Override
	public String getTyp() {
		return tmpDocLetter.getTyp();
	}

	@Override
	public void update_date(DoctorLettersTBL docLetterItem) {
		this.docLetterDao.update(docLetterItem);

	}

	@Override
	public void update_bezeichner(Long id, String bezeichner) {

		DoctorLettersTBL tmpDoc_1 = this.docLetterDao
				.retrieveDoctorLetterByID(id);
		tmpDoc_1.setBezeichner(bezeichner);
		this.docLetterDao.update(tmpDoc_1);

	}

	@Override
	public void update_date(Long id, Calendar datum) {

		DoctorLettersTBL tmpDoc_1 = this.docLetterDao
				.retrieveDoctorLetterByID(id);
		tmpDoc_1.setDatum(datum);
		this.docLetterDao.update(tmpDoc_1);

	}

	@Override
	public void update_text(Long id, String text) {

		DoctorLettersTBL tmpDoc_1 = this.docLetterDao
				.retrieveDoctorLetterByID(id);
		tmpDoc_1.setText(text);
		this.docLetterDao.update(tmpDoc_1);

	}

}
